<!-- BEGIN ANALYTICS TAB -->
<% if @actionPage.id -%>
  <div class="tab-pane" id="analytics">
    <div class="row">
      <div class="col-sm-6">
        <span class='alert alert-warning times-notification'><i class="icon-attention"></i> All dates and times are given in <%= Time.zone.name %></span>
      </div>
      <div class="col-sm-6 text-right">
        <%= render 'admin/action_pages/date_range_form' -%>
      </div>
    </div>
    <hr />
    <div class="row">
      <div class="col-sm-12">
        <h3>Action Page Views</h3>
        <%= if @date_start == @date_end
          @data = @actionPage.events.views.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
        else
          @data = @actionPage.events.views.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
        end
        line_chart(
          [{
            name: "Total Page Views",
            data: @data
          }],
          library: {canvasDimensions: {width: 800, height: 300}}
        ) %>

        <% if @actionPage.enable_email? && @actionPage.email_campaign %>
          <h3>Email Tool</h3>
          <%= if @date_start == @date_end
            @data = @actionPage.events.emails.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
          else
            @data = @actionPage.events.emails.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
          end
          line_chart(
            [{
              name: "Total Emails",
              data: @data
            }],
            library: {canvasDimensions: {width: 800, height: 300}}
          ) %>
        <% end %>

        <% if @actionPage.enable_congress_message? && @actionPage.congress_message_campaign %>
          <h3>Congress Messages Tool</h3>
          <%= if @date_start == @date_end
            @data = @actionPage.events.congress_messages.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
          else
            @data = @actionPage.events.congress_messages.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
          end
          line_chart(
            [{
              name: "Total Congress Messages",
              data: @data
            }],
            library: {canvasDimensions: {width: 800, height: 300}}
          ) %>

          <h3>Congresss Messages Sent</h3>
          <%= link_to date_tabulation_admin_congress_message_campaign_path(@actionPage.congress_message_campaign),
            target: "_blank" do -%>
            <button type='button' class='btn btn-default'>Table View</button>
          <% end -%>
          <%= link_to congress_tabulation_admin_congress_message_campaign_path(@actionPage.congress_message_campaign),
            target: "_blank" do -%>
            <button type='button' class='btn btn-default'>Congressional Breakdown</button>
          <% end -%>
          <%= if @date_start == @date_end
            line_chart(
              congress_forms_hour_fills_url(@actionPage.congress_message_campaign.campaign_tag, @date_start),
              library: {canvasDimensions: {width: 800, height: 300}}
            )
          else
            line_chart(
              congress_forms_date_fills_url(@actionPage.congress_message_campaign.campaign_tag, @date_start, @date_end),
              library: {canvasDimensions: {width: 800, height: 300}}
            )
          end %>
        <% end %>

        <% if @actionPage.enable_call? && @actionPage.call_campaign %>
          <h3>Call Tool</h3>
          <%= if @date_start == @date_end
            @data = @actionPage.events.calls.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
          else
            @data = @actionPage.events.calls.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
          end
          line_chart(
            [{
              name: "Total Calls",
              data: @data
            }],
            library: {canvasDimensions: {width: 800, height: 300}}
          ) %>
        <% end %>

        <% if @actionPage.enable_tweet? && @actionPage.tweet %>
          <h3>Tweet Tool</h3>
          <%= if @date_start == @date_end
            @data = @actionPage.events.tweets.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
          else
            @data = @actionPage.events.tweets.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
          end
          line_chart(
            [{
              name: "Total Tweets",
              data: @data
            }],
            library: {canvasDimensions: {width: 800, height: 300}}
          ) %>
        <% end %>

        <% if @actionPage.enable_petition? && @actionPage.petition && @actionPage.petition.persisted? %>
          <h3 class=clearfix>
            Petition Tool
            <% if can?(:export_signatures) %>
              <%= link_to 'Download CSV',
                          csv_admin_petition_path(@actionPage.petition),
                          class: 'btn btn-default btn-sm pull-right'-%>
            <% end %>
          </h3>
          <%= link_to 'View Signatures',
            admin_petition_path(@actionPage.petition),
            class: 'pull-right'-%>
          <%= if @date_start == @date_end
            @data = @actionPage.events.signatures.where('time BETWEEN ? AND ?', @date_start, @date_end + 1.day).group_by_hour(:time, format: '%Y-%m-%d %H:%M:00 UTC').count
          else
            @data = @actionPage.events.signatures.where('time BETWEEN ? AND ?', @date_start, @date_end).group_by_day(:time, format: '%Y-%m-%d 00:00:00 UTC').count
          end
          line_chart(
            [{
              name: "Total Signatures",
              data: @data
            }],
            library: {canvasDimensions: {width: 800, height: 300}}
          ) %>
        <% end %>

        <div class="clearfix"> </div>
      </div><!-- .col-sm-12 -->
    </div><!-- .row -->
  </div><!-- .tab-pane -->
<% end -%>
<!-- TABEND-->
